package com.wengyingjian.weixin.dispatcher;

import com.wengyingjian.kylin.util.XmlUtil;
import com.wengyingjian.weixin.alimama.TbkUrlQQTransferService;
import com.wengyingjian.weixin.common.enums.WeixinRequestMessageType;
import com.wengyingjian.weixin.common.model.WeixinRequstTextMessage;
import com.wengyingjian.weixin.common.model.WeixinResponseTextMessage;
import com.wengyingjian.weixin.common.model.WxRequestRecord;
import com.wengyingjian.weixin.common.service.WeixinTextResponseService;
import com.wengyingjian.weixin.dao.TbkUrlTransferDao;
import com.wengyingjian.weixin.dao.WxRequestRecordDao;
import com.wengyingjian.weixin.filter.generic.WeixinInterceptor;
import com.wengyingjian.weixin.util.WeixinMessageResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by wengyingjian on 16/2/19.
 */
@Service
public class WeixinTextResponseServiceImpl implements WeixinTextResponseService {

    Logger logger = LoggerFactory.getLogger(WeixinTextResponseServiceImpl.class);

    @Autowired
    private List<WeixinInterceptor> weixinInterceptorList;

    // @Resource(name = "tbkUrlQQTransferServiceImpl")
    // private TbkUrlTransferService tbkUrlTransferService;
    @Autowired
    private TbkUrlQQTransferService tbkUrlQQTransferService;

    @Autowired
    private TbkUrlTransferDao tbkUrlTransferDao;

    @Autowired
    private WxRequestRecordDao wxRequestRecordDao;

    @Override
    public boolean matches(String requestContent) {
        String type = WeixinMessageResolver.resolveRequestMessageType(requestContent);
        logger.info("WeixinTextResponseService match type = [{}]", type);
        return WeixinRequestMessageType.DEFAULT_TEXT.getType().equals(type) ? true : false;
    }

    @Override
    public String handleMessage(String requestContent) {
        // 1.将xml格式的信息转化为文本类型的request对象
        WeixinRequstTextMessage weixinRequstTextMessage = XmlUtil.fromXml(requestContent,
                WeixinRequstTextMessage.class);

        // 2.包装resp对象
        WeixinResponseTextMessage textResp = new WeixinResponseTextMessage();
        textResp.wrapper(weixinRequstTextMessage);
        // 3.获取返回文本,具体逻辑委托给doHandleMessage处理
        textResp.setContent(doHandleMessage(weixinRequstTextMessage));

        // 4.将resp对象封装成weixin能识别的xml返回
        return XmlUtil.toXml(textResp);
    }

    // private String doHandleMessage(WeixinRequstTextMessage weixinRequstTextMessage) {
    // //1.提取消息内容content
    // String requestContent = weixinRequstTextMessage.getContent();
    // return tbkUrlTransferService.getWeixinResp(requestContent);
    // }

    public String doHandleMessage(WeixinRequstTextMessage msg) {
        logger.info("处理微信请求：{}", msg);
        boolean isFirstAdd = addWxRecord(msg);
        if (tbkUrlQQTransferService.isUrlContent(msg.getContent())) {
            return tbkUrlQQTransferService.getResponse(msg.getMsgId(), msg.getContent(), isFirstAdd);
        }
        return "不是url链接内容，以文字形式处理";
    }

    public boolean addWxRecord(WeixinRequstTextMessage msg) {
        try {
            WxRequestRecord insert = new WxRequestRecord();
            insert.setMsgId(msg.getMsgId());
            insert.setContent(msg.getContent());
            insert.setRepeatCount(1);
            wxRequestRecordDao.insert(insert);
            return true;
        } catch (DuplicateKeyException e) {
            WxRequestRecord record = wxRequestRecordDao.selectByMsgId(msg.getMsgId());
            wxRequestRecordDao.addRepeatCountById(record.getId());
            return false;
        }
    }

}
